package createpattern.prototype;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author seer
 * @date 2018/2/7 11:45
 */
public class ShapeCache {

    private static ConcurrentHashMap<String, Shape> shapeMap = new ConcurrentHashMap<>();

    public static Shape getShape(Class<? extends Shape> clazz) throws CloneNotSupportedException {
        Shape cacheShape = shapeMap.get(clazz.getName());
        return ((Shape) cacheShape.clone());
    }

    /**
     * 对每种形状都运行数据库查询，并创建该形状
     */
    public static void loadShape() {
        Rectangle rectangle
                = new Rectangle();
        rectangle.setName("rec");
        shapeMap.put(Rectangle.class.getName(), rectangle);

        Square square
                = new Square();
        square.setName("squ");
        shapeMap.put(Square.class.getName(), square);
    }
}
